package com.qfedu.knowledge.job;

import com.qfedu.knowledge.entity.bo.QuestionInfoBO;
import com.qfedu.knowledge.entity.dto.AddQuestion4ESDTO;
import com.qfedu.knowledge.service.IQuestionInfoService;
import com.qfedu.knowledge.util.ElasticSearchUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 定时任务
 *
 * @ClassName Schedule
 * @Author ykx
 * @Date 2024/1/5 10:13
 * @Version 1.0
 */
@Component
public class Schedule {

    @Autowired
    private IQuestionInfoService questionInfoService;

    @Autowired
    private ElasticSearchUtils elasticSearchUtils;

    @Scheduled(cron = "0 0 12 * * ?")
    public void syncToEs() {
        List<QuestionInfoBO> list = questionInfoService.list();
        List<AddQuestion4ESDTO> addQuestion4ESDTOS = new ArrayList<>();
        for (QuestionInfoBO questionInfoBO : list) {
            addQuestion4ESDTOS.add(AddQuestion4ESDTO.buildQuestion4ES(questionInfoBO));
        }

        elasticSearchUtils.addBatch(addQuestion4ESDTOS);
    }
}
